//
//  YKAddTagToolbar.m
//  baisi
//
//  Created by 吴高烽 on 2016/10/31.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKAddTagToolbar.h"
#import "YKAddTagViewController.h"
#import "YKTagButton.h"
@interface YKAddTagToolbar ()
/** 顶部控件 */
@property (weak, nonatomic) IBOutlet UIView *topView;
/** 添加按钮 */
@property (nonatomic ,weak)UIButton *addButton;
/** 存放所有的标签label */
@property (nonatomic ,strong)NSMutableArray *tagLabels;

@end

@implementation YKAddTagToolbar
-(NSMutableArray *)tagLabels{
    if (!_tagLabels) {
//        _tagLabels = [NSMutableArray arrayWithObjects:@"吐槽",@"糗事",nil];
        _tagLabels  = [NSMutableArray arrayWithCapacity:2];
    }
    return _tagLabels ;
}
+(instancetype)addTagToolbar{
    return  [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]lastObject];
    
}
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setupAddButton];
    [self creatTags:@[@"糗事",@"吐槽"]];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    //    只设置了size，先设置文字后再计算
    for (NSInteger i = 0 ; i < self.tagLabels.count; i++) {
        UILabel *tagLabel =self.tagLabels[i];
        if (i==0) {
            tagLabel.x = 0 ;
            tagLabel.y = 0 ;
        }else{
            UILabel *lastTagLabel = self.tagLabels[i-1];
            CGFloat leftWidth = CGRectGetMaxX(lastTagLabel.frame) + YKTagButtonMargin ;
            CGFloat rightWidth = self.topView.width - leftWidth ;
            if (rightWidth >= lastTagLabel.width) {
                tagLabel.y = lastTagLabel.y;
                tagLabel.x = leftWidth ;
            }else{
                tagLabel.x = 0 ;
                tagLabel.y = CGRectGetMaxY(lastTagLabel.frame ) + YKTagButtonMargin ;
            }
        }
    }
    UILabel *lastTagLabel = [self.tagLabels lastObject];
    CGFloat leftWidth = CGRectGetMaxX(lastTagLabel.frame) + YKTagButtonMargin ;
    if (self.topView.width - leftWidth >= self.addButton.width) {
        self.addButton.y = lastTagLabel.y;
        self.addButton.x = leftWidth ;
    }else{
        self.addButton.x = 0 ;
        self.addButton.y = CGRectGetMaxY(lastTagLabel.frame) + YKTagButtonMargin ;
    }
    CGFloat oldH = self.height ;
    self.height = CGRectGetMaxY(lastTagLabel.frame) + 45 ;
    self.y -= self.height - oldH ;
}
-(void)setupAddButton{
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn addTarget:self action:@selector(addButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [addBtn setImage:[UIImage imageNamed:@"tag_add_icon"] forState:UIControlStateNormal];
    addBtn.size = addBtn.currentImage.size ;
    addBtn.x = YKTagButtonMargin ;
    [self.topView addSubview:addBtn];
    self.addButton = addBtn ;
}
-(void)addButtonClick:(UIButton*)btn{
    YKAddTagViewController *addVC = [[YKAddTagViewController alloc]init];
   __weak typeof(self) weakSelf = self;
    addVC.tagsBlock =^(NSArray *tags){
        [weakSelf creatTags:tags];
    };
    addVC.tags = [self.tagLabels valueForKeyPath:@"text"];
    UIViewController *rootVC = YKKeyWindow.rootViewController ;
    UINavigationController *nav = (UINavigationController *)rootVC.presentedViewController ;
//    [nav presentViewController:addVC animated:YES completion:nil];
    [nav pushViewController:addVC animated:YES];
}
-(void)creatTags:(NSArray<NSString*>*)tags{
    [self.tagLabels makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.tagLabels removeAllObjects];
//    只设置了size，先设置文字后再计算
    for (NSInteger i = 0 ; i < tags.count; i++) {
        UILabel *tagLabel = [[UILabel alloc]init];
        [self.tagLabels addObject:tagLabel];
        tagLabel.backgroundColor = YKTagBackgroundColor ;
        tagLabel.text = tags[i];
        tagLabel.font = YKTagFont ;
        [tagLabel sizeToFit];
        tagLabel.width += 2 * YKTagButtonMargin ;
        tagLabel.height = YKTagButtonH ;
        tagLabel.textColor = [UIColor whiteColor];
        tagLabel.textAlignment = NSTextAlignmentCenter ;
        [self.topView addSubview: tagLabel];
    }
    [self setNeedsLayout];
}


@end
